namespace Core.Features
{
    using System;
    using Base;
    using Contracts.Enums;
    using Resources;
    using System.Linq;


    public sealed class ArmorAmount<T> : FeatureBase<object>
    {
        public ArmorAmount(){}

        public ArmorAmount(T value) 
            : base(value){ }
        /// <summary>
        /// Obtains the feature name
        /// </summary>
        public override string Name
        {
            get { return "Armor capacity"; }
        }
        /// <summary>
        /// Obtains the feature group
        /// </summary>
        public override FeatureGroupsEnum FeatureGroup{ get { return FeatureGroupsEnum.Defence; } }
        /// <summary>
        /// Obtains the feature type
        /// </summary>
        public override FeaturesEnum FeatureType { get { return FeaturesEnum.ArmorAmount; } }
        /// <summary>
        /// Obtains or defines the feature unit
        /// </summary>
        public override string Unit { get { return Traduction.label_unit_hp; } }

        /// <summary>
        /// Determines if the feature value is rounded
        /// </summary>
        public override bool IsRounded
        {
            get { return true; }
        }

        public override void SetModifiedValue(Contracts.Interfaces.IAgent agent, Contracts.Interfaces.IRobot robot)
        {
            if (agent == null || robot == null) return;
            
            // Bonuses from extensions
            var extensionBonus = agent.Extensions.SumModifiersValues(FeatureTypesEnum.Robot, this.FeatureType);
            
            // Bonuses from bot
            var robotBonus = robot.Modifiers.SumModifiersValues(agent.Extensions, FeatureTypesEnum.Robot, this.FeatureType);

            // Bonus from armor plates
            var armorPlatesBonus = robot.Modules.SumModifiersValues(FeatureTypesEnum.Robot, this.FeatureType, ModuleGroupsEnum.ArmorPlates);

            var temp = ((double)Value * (1 + (extensionBonus + robotBonus))) + armorPlatesBonus;

            // Malus from LightweightFrame
            var lwf = robot.Modules.Where(m => m.Group == ModuleGroupsEnum.LightweightFrames).FirstOrDefault();
            if(lwf != null)
            {
                var mods = lwf.Modifiers.Where(m => m.Feature == this.FeatureType && m.ModifiedFeatureType == FeatureTypesEnum.Robot);
                if (mods.Any())
                    temp = mods.Aggregate(temp, (current, modifier) => current * (1 - Math.Abs(modifier.Value)));
            }

            ModifiedValue = IsRounded
                ? Math.Round(temp, 0, MidpointRounding.AwayFromZero)
                : Math.Round(temp, 2);
        }
    }
}